﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace TrackingAssist.WebTrends
{
    public class WebTrends:ICloneable
    {
        public MatchResult Match;
        public WebTrendsItem Section;
        public WebTrendsItem Location;
        public WebTrendsItem Action;
        public WebTrendsItem ActionName;
        public WebTrendsItem Var1;
        public WebTrendsItem Var2;
        public WebTrendsItem Var3;
        
        public string Language;
        public string Market;
        public string Browser;
        public string Id;
        public string Description;
        public string FullUrl;
        public bool IsDynamic;
        public bool IsMatch;
        public string Time;
        public string VideoTime;
        public string Project;

        public WebTrends()
        {
            Match = MatchResult.NotMatch;
            Section = new WebTrendsItem();
            Location = new WebTrendsItem();
            Action = new WebTrendsItem();
            ActionName = new WebTrendsItem();
            Var1 = new WebTrendsItem();
            Var2 = new WebTrendsItem();
            Var3 = new WebTrendsItem();
          
        }

        /// <summary>
        /// Try parse a request url to Atlas object
        /// </summary>
        /// <param name="url"></param>
        /// <param name="atlas"></param>
        /// <returns></returns>
        public static bool TryParse(string url, out WebTrends webtrends)
        {
            if (url.StartsWith(WebTrendsConsts.WEBTRENDS_DOMAIN))
            {

                string[] item = url.Split(new char[] { '&' });

                webtrends = new WebTrends();
                webtrends.FullUrl = string.Concat("http://", url);
                if (item.Length > 3)
                    webtrends.Project = item[2];

                foreach (string s in item)
                {
                    int dotIndex = s.IndexOf('=');
                    if (dotIndex > 0 && dotIndex < s.Length - 1)
                    {
                        string key = s.Substring(0, dotIndex);
                        string value = s.Substring(dotIndex + 1);

                        if (WebTrendsView.EnableUrlDecode)
                        value = System.Web.HttpUtility.UrlDecode(value);

                        switch (key)
                        {
                            case "section":
                                webtrends.Section.Value = value; break;
                            case "location":
                                webtrends.Location.Value = value; break;
                            case "action":
                                webtrends.Action.Value = value; break;
                            case "actionname":
                                webtrends.ActionName.Value = value; break;
                            case "var1":
                                webtrends.Var1.Value = value; break;
                            case "var2":
                                webtrends.Var2.Value = value; break;
                            case "var3":
                                webtrends.Var3.Value = value; break;
                            case "WT.ul":
                                webtrends.Language = value; break;
                            case "WT.cg_n":
                                webtrends.Market = value; break;
                            case "browser":
                                webtrends.Browser = value; break;
                            case "SETTrackID":
                                webtrends.Id = value; break;
                            case "time":
                                webtrends.Time = value; break;
                            case "videotime":
                                webtrends.VideoTime = value; break;
                        }
                    }
                }
                WebTrendsSpecUtility.CompareActualWithSpec(webtrends);
                return true;
            }

            webtrends = null;
            return false;

        }

        public object Clone()
        {
            WebTrends webtrends = new WebTrends();
            webtrends.Section.Value = this.Section.Value;
            webtrends.Section.IsDynamic = this.Section.IsDynamic;
            webtrends.Location.Value = this.Location.Value;
            webtrends.Location.IsDynamic = this.Location.IsDynamic;
            webtrends.Action.Value = this.Action.Value;
            webtrends.Action.IsDynamic = this.Action.IsDynamic;
            webtrends.ActionName.Value = this.ActionName.Value;
            webtrends.ActionName.IsDynamic = this.ActionName.IsDynamic;
            webtrends.Var1.Value = this.Var1.Value;
            webtrends.Var1.IsDynamic = this.Var1.IsDynamic;
            webtrends.Var2.Value = this.Var2.Value;
            webtrends.Var2.IsDynamic = this.Var2.IsDynamic;
            webtrends.Var3.Value = this.Var3.Value;
            webtrends.Var3.IsDynamic = this.Var3.IsDynamic;

            webtrends.Language = this.Language;
            webtrends.Market = this.Market;
            webtrends.Browser = this.Browser;
            webtrends.Id = this.Id;
            webtrends.Description = this.Description;
            webtrends.FullUrl = this.FullUrl;
            webtrends.IsDynamic = this.IsDynamic;
            webtrends.IsMatch = this.IsMatch;
            webtrends.VideoTime = this.VideoTime;
            webtrends.Time = this.Time;
            return webtrends;
        }
    }

    

    
}